{include file="my/header"}
<div class="container-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格数据查询块 -->
            <div class="table-query">
                <form class="layui-form layui-inline J-search-form" action="">
                    <div class="layui-inline">
                        <label class="layui-form-label">搜索ID：</label>
                        <div class="layui-inline">
                            <input class="layui-input J-query-params" name="id" autocomplete="off" placeholder="请输入id" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">课程:</label>
                        <div class="layui-inline">
                            <input class="layui-input J-query-params" name="coursename" autocomplete="off" placeholder="请输入课程" />
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">考试任务:</label>
                        <div class="layui-inline">
                            <input class="layui-input J-query-params" name="name" autocomplete="off" placeholder="请输入考试任务" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">审核状态:</label>
                        <div class="layui-inline">
                            <select name="check" class="layui-input J-query-params">
                                <option value="" selected>请选择审核状态</option>
                                <option value="0">未审核</option>
                                <option value="1">已通过</option>
                                <option value="2">未通过</option>
                            </select>
                            <!-- <input class="layui-input J-query-params" name="check" autocomplete="off" placeholder="请输入内容"> -->
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">发布时间:</label>
                        <div class="layui-inline">
                            <input type="text" class="layui-input J-query-params J-date" name="create_time" placeholder="yyyy-MM-dd" />
                        </div>
                    </div>
                </form>
                <button class="layui-btn J-search" data-type="reload">搜索</button>
                <button class="layui-btn J-reload">重置</button>
            </div>
            <!-- 表格 -->
            <table class="layui-hide" id="testTable" lay-filter="test"></table>
            <!-- 表格工具 -->
            <script type="text/html" id="toolbarDemo">

                <div class="layui-btn-container">
                    {if condition="session('userclass') eq 1 or session('userclass') eq 5"}
                    <!-- <button class="layui-btn layui-btn-sm" lay-event="add">添加</button> -->
                    <button class="layui-btn layui-btn-sm" lay-event="testType">批量设置考试形式</button>
                    <button class="layui-btn layui-btn-sm" lay-event="addClass">添加课程</button>
                    {else / }
                    <button class="layui-btn layui-btn-sm" lay-event="batchdel">批量删除</button>
                    <button class="layui-btn layui-btn-sm" lay-event="batchCheck">批量审核</button>
                    {/if}
            </script>
            <script type="text/html" id="barDemo">
                {if condition="session('userclass') eq 1 or session('userclass') eq 5"}
                <!-- <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a> -->
                <a class="layui-btn layui-btn-xs" lay-event="editTestType">考试形式</a>
                {else /}
                <a class="layui-btn layui-btn-xs" lay-event="check">审核</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

                {/if}
            </script>
        </div>
        <div class="margin-top30 J-temple is-hidden">
            <form class="layui-form layui-fluid J-add-form" lay-filter="formTest">
                <input type="hidden" class="form-control" id="id" name="id" value="" placeholder="请输入内容" />
                <input type="hidden" class="form-control" id="schoolid" name="schoolid" value="985" placeholder="请输入内容" />

                <div class="layui-form-item">
                    <label class="layui-form-label">考试形式</label>
                    <div class="layui-input-block">
                        <select name="c_test_type" id="testType">
                            <option value="2">上机</option>
                            <option value="3">大作业</option>
                            <option value="10">笔试(闭)</option>
                            <option value="11">笔试(开)</option>
                        </select>
                    </div>
                </div>

                <!-- <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea name="demo" id="demo" placeholder="请输入内容" class="layui-textarea"></textarea>
                    </div>
                </div> -->
            </form>
        </div>
    </div>
</div>

<script>
    $(function () {
        initClass('courselist');
        initTable();
        layuiDate('.J-date');
    });

    function initTable() {
        var table;
        // 请求路径
        var requireUrl = {
            add: "{:url('test/courseadd')}",
            edit: "{:url('test/courseedit')}",
            remove: "{:url('test/courseremove')}",
            list: "{:url('test/getcourselist')}",
            check: "{:url('test/coursecheck')}",
            batchTestType: "{:url('test/batchtesttype')}",
            addCourse: "{:url('test/courselistadd')}"
        };

        layui.use('table', function () {
            table = layui.table;
            table.render({
                elem: '#testTable',
                url: requireUrl.list,
                toolbar: '#toolbarDemo',
                title: '考试课程',
                totalRow: false,
                cols: [
                    [
                        { type: 'checkbox', fixed: 'left' },
                        { field: 'id', title: '序号', width: 60 },
                        { field: 'coursename', title: '课程', width: 100 },
                        {
                            field: 'c_type',
                            title: '课程类别',
                            width: 100,
                            templet: d => {
                                var result;
                                d.c_type == 0 && (result = '公共课');
                                d.c_type == 1 && (result = '专业核心课');
                                d.c_type == 2 && (result = '专业基础课');
                                return result;
                            },
                        },
                        { field: 'c_time', title: '课时', width: 50 },
                        {
                            field: 'c_test_type',
                            title: '考试形式',
                            width: 100,
                            templet: d => {
                                var result;
                                d.c_test_type == 10 && (result = '笔试(闭)');
                                d.c_test_type == 11 && (result = '笔试(开)');
                                d.c_test_type == 2 && (result = '上机');
                                d.c_test_type == 3 && (result = '大作业');
                                return result;
                            },
                        },
                        {
                            field: 'c_orange_type',
                            title: '组织形式',
                            templet: d => {
                                var result;
                                d.c_orange_type == 0 && (result = '<span class="">随堂考</span>');
                                d.c_orange_type == 1 && (result = '<span class="">统考</span>');
                                return result;
                            },
                        },
                        { field: 'name', title: '考试任务', width: 200 },
                        {
                            field: 'check',
                            title: '审核状态',
                            templet: d => {
                                var result;
                                d.check == 0 && (result = '<span class="">未审核</span>');
                                d.check == 1 && (result = '<span class="green">通过</span>');
                                d.check == 2 && (result = '<span class="red">不通过</span>');
                                return result;
                            },
                        },
                        { field: 'demo', title: '备注', width: 200 },
                        { field: 'create_time', title: '发布时间', width: 200 },
                        { fixed: 'right', title: '操作', toolbar: '#barDemo' },
                    ],
                ],
                id: 'testReload',
                page: true,
                parseData: function (res) {
                    return {
                        code: 0,
                        msg: '',
                        count: res.length,
                        data: res,
                    };
                },
            });

            //工具栏事件
            table.on('toolbar(test)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                switch (obj.event) {
                    case 'testType':
                        if (data.length == 0) {
                            layer.msg('请至少选择一条记录！', { icon: 7 });
                            return;
                        }

                        var delList = [];
                        for (var i = 0; i < data.length; i++) {
                            delList.push(data[i].id);
                        }

                        var ids = JSON.stringify(delList);
                        layerOpenInit('批量设置考试形式', $('.J-temple'), function (index) {
                            var params = {
                                ids: ids,
                                c_test_type: $('#testType').val(),
                            };
                            ajaxRequest('post', requireUrl.batchTestType, params, function (data) {
                                data.code == 1 ? successTrip(data) : errorAlert(false, data.msg);
                            });
                        });

                        break;
                    case 'addClass':
                        openFrame(requireUrl.addCourse + '?taskid=' + data.id, '添加考试课程', ['1000px', '665px'])
                        break;
                    case 'batchdel':
                        tableBatchDel(checkStatus, requireUrl.remove);
                        break;
                    case 'batchCheck':
                        if (data.length == 0) {
                            layer.msg('请至少选择一条记录！', { icon: 7 });
                            return;
                        }

                        var delList = [];
                        for (var i = 0; i < data.length; i++) {
                            delList.push(data[i].id);
                        }

                        var data = { ids: JSON.stringify(delList) };
                        layerOpenInit(
                            {
                                title: '审核',
                                btnTitle: ['通过', '不通过'],
                            },
                            `<h3 style="padding: 20px;">是否通过所选课程？</h3>`,
                            function (index) {
                                data.check = 1;
                                ajaxRequest('post', requireUrl.check, data, function (data) {
                                    data.code == 1 ? successTrip(data) : errorAlert(false, data.msg);
                                });
                            }, null, function () {
                                data.check = 2;
                                ajaxRequest('post', requireUrl.check, data, function (data) {
                                    data.code == 1 ? successTrip(data) : errorAlert(false, data.msg);
                                });
                            }
                        );
                        break;
                }
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layerConfirm(requireUrl.remove, {
                        id: data.id,
                    });
                } else if (obj.event === 'editTestType') {
                    layerOpenInit(
                        '设置考试形式',
                        $('.J-temple'),
                        function (index) {
                            var data = $('form').serialize();
                            ajaxRequest('post', requireUrl.edit, data, function (data) {
                                data.code == 1 ? successTrip(data) : errorAlert(false, data.msg);
                            });
                        },
                        function () {
                            ajaxRequest(
                                'get',
                                requireUrl.edit,
                                {
                                    id: data.id,
                                },
                                function (res) {
                                    form.val('formTest', res.data);
                                }
                            );
                        }
                    );
                } else if (obj.event === 'check') {
                    var data = {
                        id: data.id,
                    };

                    layerOpenInit(
                        {
                            title: '审核',
                            btnTitle: ['通过', '不通过'],
                        },
                        `<h3 style="padding: 20px;">是否通过所选课程？</h3>`,
                        function (index) {
                            data.check = 1;
                            ajaxRequest('get', requireUrl.check, data, function (data) {
                                data.code == 1 ? successTrip(data) : errorAlert(false, data.msg);
                            });
                        }, null,
                        function () {
                            data.check = 2;
                            ajaxRequest('get', requireUrl.check, data, function (data) {
                                data.code == 1 ? successTrip(data) : errorAlert(false, data.msg);
                            });
                        }
                    );
                }
            });
        });

        // 搜索重置操作
        var active = {
            reload: function () {
                var queryParams = serializeObject($('.J-search-form'));
                console.log(queryParams);
                //执行重载
                table.reload(
                    'testReload',
                    {
                        page: {
                            curr: 1, //重新从第 1 页开始
                        },
                        where: queryParams,
                    },
                    'data'
                );
            },
        };
        $('.J-search').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $('.J-reload').on('click', function () {
            $('.J-query-params').val('');
            form.render('select');
            active.reload ? active.reload.call(this) : '';
        });
    }
</script>

{include file="my/footer"}